package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.data.ResponseStausEnum;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.service.BrandService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Description
 * @ClassName BrandController
 * @Author RC
 * @date 2020.12.09 19:28
 */
@Controller
@RequestMapping("brand")
public class BrandController {

    @Reference(timeout = 1000000,check = false)
    private BrandService brandService;
    private static final Logger logger = LoggerFactory.getLogger(BrandController.class);

  @RequestMapping("getBrandList")
  @ResponseBody
  public DataResult getBrandList(){
      try{
          logger.info("进入查询品牌方法");
          List<TbBrand> brandList =  brandService.brandList();
          return DataResult.response(ResponseStausEnum.SUSSCESS).setData(brandList);

      }catch (Exception e){
          logger.info("查询失败",e);
          return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
      }

  }

    @RequestMapping("saveBrand")
    @ResponseBody
    public DataResult saveBrand(TbBrand tbBrand){
        //保存  数据有空值 数据库保存异常 ,业务方面导致的没有添加成功 falise
        //可以使用map
        try{
            logger.info("进入增加品牌方法");
            boolean b =  brandService.saveBrand(tbBrand);
            return DataResult.response(ResponseStausEnum.SUSSCESS);

        }catch (Exception e){
            logger.info("增加失败",e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }

    }

    @RequestMapping("toUpdate/{id}")
    @ResponseBody
    public DataResult toUpdate(@PathVariable("id")Integer id, Model model){
        try {
            logger.info("进入回显方法");
            TbBrand tbBrand = brandService.getBrandById(id);
            return DataResult.response(ResponseStausEnum.SUSSCESS).setData(tbBrand);
        }catch (Exception e){
            logger.info("回显失败 ",e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }
    }

   @RequestMapping("updateBrand")
   @ResponseBody
   public DataResult updateBrand(TbBrand tbBrand){
       try {
           logger.info("进入修改方法");
            brandService.updateBrand(tbBrand);
           return DataResult.response(ResponseStausEnum.SUSSCESS);
       }catch (Exception e){
           logger.info("修改失败 ",e);
           return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
       }
   }


    @RequestMapping("deleteBatchBrand/{ids}")
    @ResponseBody
    public DataResult deleteBatchBrand(@PathVariable("ids")Integer[] ids){
        try{
            logger.info("进入删除方法");
            boolean b = brandService.deleteBatchBrand(ids);
            return DataResult.response(ResponseStausEnum.SUSSCESS);

        }catch (Exception e){
            logger.info("删除失败",e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }
    }

}
